package me.chan.extra;

import me.chan.sort.InsertionSort;
import me.chan.sort.QuickSort;
import me.chan.sort.SelectionSort;
import me.chan.sort.ShellSort;
import me.chan.std.StdOut;
import me.chan.std.StdRandom;
import me.chan.std.Stopwatch;

/**
 * Created by JanGin on 2017/9/10.
 */
public class SortCompare {

    public static double time(String alg, Double[] a) {
        Stopwatch watch = new Stopwatch();
        if (alg.equalsIgnoreCase("Insertion")) InsertionSort.sort(a);
        if (alg.equalsIgnoreCase("Selection")) SelectionSort.sort(a);
        if (alg.equalsIgnoreCase("Shell")) ShellSort.sort(a);
        if (alg.equalsIgnoreCase("Quick")) QuickSort.sort(a);


        return watch.elapsedTime();
    }

    public static double timeRandomInput(String alg, int N, int T) {
        double total = 0.0;

        Double[] a = new Double[N];
        for (int t=0; t < T; t++) {
            for (int i=0; i < N; i++) {
                a[i] = StdRandom.uniform();
            }
            total += time(alg ,a);
        }

        return total;
    }

    public static void main(String[] args) {
        String alg1 = args[0];
        String alg2 = args[1];

        int N = Integer.parseInt(args[2]);
        int T = Integer.parseInt(args[3]);

        double t1 = timeRandomInput(alg1, N, T);
        double t2 = timeRandomInput(alg2, N, T);

        StdOut.println(t1);
        StdOut.println(t2);

        StdOut.printf("For %d random Doubles\n %s is ", N, alg1);
        StdOut.printf("%.2f times faster than %s\n", t2/t1, alg2);
    }
}
